package com.example.logindb.controller;



import com.example.logindb.dto.ResponseMessage;
import com.example.logindb.entity.Help;
import com.example.logindb.entity.Online;
import com.example.logindb.entity.User;
import com.example.logindb.sevices.HelpService;
import com.example.logindb.sevices.OnlineService;
import com.example.logindb.sevices.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


@RestController
@RequestMapping("/online")
public class OnlineController {

    private OnlineService onlineService;
    private UserService userService;
    private HelpService helpService;
    @Autowired
    public OnlineController(OnlineService onlineService, UserService userService, HelpService helpService) {
        this.onlineService = onlineService;
        this.userService = userService;
        this.helpService = helpService;
    }


    //更新在线状态
    @PostMapping
    public ResponseMessage<Online> updateStatus(@RequestBody Online online)
    {
        onlineService.updateStatus(online);
        //查询是否为盲人离线的状态
        if(online.getStatus()==0){
            User user = userService.findAllById(online.getId());
            if(!Objects.equals(user.getUserrole(), "blind")){
                List<Help> helps=helpService.findByBid(online.getId());
                for (Help value : helps) {
                    if (value.getVid() == 0) {
                        value.setVid(1);
                        helpService.updateblind(value);
                    }
                }
            }
        }

        return ResponseMessage.success(online);
    }

    //查询在线状态
    @GetMapping("/{id}")
    public ResponseMessage<Online> get_relation(@PathVariable Integer id)
    {
        return ResponseMessage.success(onlineService.findStatusById(id));
    }



}
